

import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'dart:convert';

class MorePage extends StatefulWidget {
  const MorePage({Key? key}) : super(key: key);

  @override
  State<MorePage> createState() => _MorePageState();
}

class _MorePageState extends State<MorePage> {
  ScrollController _controller = ScrollController(); // listview的控制器

  List _list = [];
  int _page = 1;
  bool isLoading = false; // 是否正在加载数据

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    this._getData();
    _controller.addListener(() {
      if(_controller.position.pixels > _controller.position.maxScrollExtent -20){
        print("滑动了最底部");
        _getData();
      }
    });
  }

  _getData() async{
    var apiUrl = "http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=${this._page}";
    Response ressult = await Dio().get(apiUrl);
    print(json.decode(ressult.data)["result"]);
    setState(() {
      this._list.addAll(json.decode(ressult.data)["result"]);
      this._page++;
    });
  }

  // 加载更多时显示的组件，用户提示
  Widget _getMoreWidget(){
    return Center(
      child:Padding(
        padding: EdgeInsets.all(10.0),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Text(
              "加载中....",
              style: TextStyle(
                fontSize: 16.0
              ),
            ),
            CircularProgressIndicator(  //??
              strokeWidth: 1.0,
            )
          ],
        ),
      )
    );
  }

  Future<void> _onRefresh() async{
    print("执行刷新");
    await Future.delayed(Duration(seconds: 3),(){
      print("refresh");
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("下拉刷新 上拉分页加载更多"),
      ),
      body: this._list.length >0?
        RefreshIndicator(
          onRefresh: _onRefresh,
          child: ListView.builder(
            itemCount: this._list.length,
            controller: _controller,
            itemBuilder: (context,index){
              if(index == this._list.length-1){
                return Column(
                  children: [
                    ListTile(
                      title: Text(this._list[index]["title"],
                      maxLines: 1,
                      ),
                      onTap: (){
                       // Navigator.pushNamed(context, "/newsContent");
                      },
                    ),
                    Divider(),
                    _getMoreWidget()
                  ],
                );
              }else{
                return Column(
                  children: [
                    ListTile(
                      title: Text(this._list[index]["title"],maxLines: 1,),
                      onTap: (){},
                    ),
                    Divider()
                  ],
                );

              }
            }),
          ):_getMoreWidget()
      
    );
    
  }
}